import java.awt.Color;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

/**
 * Create a table that is 20x20 by default
 * @author jcoston
 */
public class Table extends JPanel {
	private static final long serialVersionUID = 1L;
	private CSVModel model = null;
	private JTable table = null;
	private JScrollPane scrollPane = null;

	/**
	 * Create a table with the data model m
	 * @param m The data model
	 */
	public Table(CSVModel m) {
		//Set the table's model
		setModel(m);
		table = new JTable(getModel());
		//Show selected cell in red
		table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		table.setSelectionBackground(Color.red);
		//table.setPreferredScrollableViewportSize(new Dimension(800, 300));
		table.setFillsViewportHeight(true);
		table.setGridColor(Color.blue);
		table.setShowGrid(true);
		table.setCellSelectionEnabled(true);
		//Place table in scroll pane
		scrollPane = new JScrollPane(table);
		add(scrollPane);
		
		//Create the table (20x20)
		model.create(20,20);
	}

	/**
	 * Sets the current model.
	 * @param m the new model.
	 */
	public void setModel(CSVModel m) {
		if (model != null) {
			//model.removeTableModelListener(this);
		}
		model = m;
		if (model != null) {
			//model.addTableModelListener(this);
		}
	}

	/**
	 * Gets the current model used.
	 * @return the current model.
	 */
	public CSVModel getModel() {
		return model;
	}

}
